#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: 李吉凯
@Email: lijk34925@hundsun.com
@Time: 2023/2/27 17:29
@File: jsonOperation.py
"""
import json

from BladeFerm.Lib.Common import logger


class JsonOperation:
    def __init__(self):
        self.content = None

    def read_json(self, path, encoding='gbk'):
        # 打开文件,r是读取,encoding是指定编码格式
        with open(path, 'r', encoding=encoding) as fp:
            self.content = json.load(fp)
            # load()函数将fp(一个支持.read()的文件类对象，包含一个JSON文档)反序列化为一个Python对象
            logger.info(f'content: {self.content}')
        return self.content

    @staticmethod
    def write_json(content, path, encoding='utf-8'):
        with open(path, 'w', encoding=encoding) as fp:
            json.dump(content, fp)
            # load()函数将fp(一个支持.read()的文件类对象，包含一个JSON文档)反序列化为一个Python对象
            logger.info(f'{fp} write ok')